-- All the Settings and their default values
FG.settingsDefault = {
	info = 'the default classic PvPvE Facility Gamemode settings.',
	author = 'Sharp-Shark',
	gamemode = 'default',
	monsterSpawn = 'default',
	friendlyFire = false,
	allowEnd = true,
	allowEndMinPlayers = 2,
	autoJob = true,
	autoJobMinPlayers = 2,
	autoJobRoleSequence = 'xsiig-xisig',
	decontaminationTimer = 60*12 + 15,
	terroristTickets = 3.5,
	nexpharmaTickets = 4.5,
	respawnType = 'default',
	respawnSpeed = 16.5,
	respawnAccel = 1.5,
	initialTrollPercentage = 20,
	conversionTrollPercentage = 20
}

-- For FG Config Editor GUI - for string settings which have a set of predefined values
FG.settingsDropdown = {
	gamemode = {
		'default',
		'husk',
		'greenskin'
	},
	monsterSpawn = {
		'default',
		'staff'
	},
	respawnType = {
		'default',
		'split',
		'infiniteguards'
	}
}

-- For FG Config Editor GUI - for string settings which only have some legal characters
FG.settingsLegalChars = {
	autoJobRoleSequence = 'xosegijm-'
}

-- For FG Config Editor GUI - description for each setting
FG.settingsDescription = {
	info = '你的预设配置说明',
	author = '该预设配置的创建者',
	gamemode = '游戏模式',
	monsterSpawn = '决定怪物将被放置在哪个生成点',
	friendlyFire = '允许友军伤害',
	allowEnd = '巡回是否可以结束',
	allowEndMinPlayers = '结束巡回的最低玩家数量。优先于"allowEnd"',
	autoJob = '职业平衡是否由脚本覆写',
	autoJobMinPlayers = '职业平衡的最低玩家数量。优先于"autoJob"',
	autoJobRoleSequence = '分配给玩家的职业序列',
	decontaminationTimer = '设施净化倒计时',
	terroristTickets = 'T阵营的初始入场券数量',
	nexpharmaTickets = 'Nex制药公司的初始入场券数量',
	respawnType = '决定使用的重生系统',
	respawnSpeed = '决定重生速度倍率',
	respawnAccel = '决定每次重生后的等待时间递增值',
	initialTrollPercentage = '巡回开始后哥布林变成巨魔的概率',
	conversionTrollPercentage = '在被转换成哥布林后变成巨魔的概率'
}

-- Prepackaged Settings Presets or "SubGameModes"
FG.settingsPresetsDefault = {
	default = FG.settingsDefault,
	['nomonsters'] = {
		info = '没有怪物的默认模式',
		author = 'Sharp-Shark',
		autoJobRoleSequence = 'siig-isig'
	},
	['huskmode'] = {
		info = '怪物是由玩家控制的感染体',
		author = 'Sharp-Shark',
		gamemode = 'husk'
	},
	['jetinvasion'] = {
		info = 'PVP模式，Nex工作人员、警卫抵御朱庇特精英部队（JET）的进攻',
		author = 'Sharp-Shark',
		autoJobRoleSequence = 'jogs',
		terroristTickets = 0,
		nexpharmaTickets = 99,
		respawnType = 'split'
	},
	['prisonescape'] = {
		info = 'PVP模式，警卫必须杀死所有的逃犯',
		author = 'Sharp-Shark',
		autoJobRoleSequence = 'igie',
		terroristTickets = 4,
		nexpharmaTickets = 0.5
	},
	['scp:cb'] = {
		info = '灵感源自SCP:CB，JET、DD对抗怪物、MERCS',
		author = 'SonicHegehodge',
		autoJobRoleSequence = 'xiiji-xji',
		terroristTickets = 0,
		nexpharmaTickets = 3,
		respawnSpeed = 10
	},
	['surfacetension'] = {
		info = '无尽团队PVP模式，朱庇特精英部队（JET） VS MERCS',
		author = 'Sharp-Shark',
		autoJobRoleSequence = 'mj',
		decontaminationTimer = 60*0.2 + 15,
		terroristTickets = 1.5,
		nexpharmaTickets = 1.5,
		respawnType = 'split',
		respawnSpeed = 999,
		respawnAccel = 1
	},
	['ffa'] = {
		info = '无规则的警卫混战模式，无需净化设施。',
		author = 'Sharp-Shark',
		friendlyFire = true,
		allowEnd = false,
		autoJobMinPlayers = 0,
		autoJobRoleSequence = 'g',
		decontaminationTimer = 999999,
		respawnType = 'infiniteguards',
		respawnSpeed = 999,
		respawnAccel = 1
	},
	['trollmode'] = {
		info = '怪物是由玩家控制的巨魔，一种愚蠢但强大的哥布林亚种',
		author = 'Sharp-Shark',
		gamemode = 'greenskin',
		initialTrollPercentage = 100
	},
	['greenskinlair'] = {
		info = '设施已被绿皮怪物占领，朱庇特精英部队（JET）前来摧毁这里的怪物巢穴',
		author = 'Sharp-Shark',
		gamemode = 'greenskin',
		monsterSpawn = 'staff',
		autoJobRoleSequence = 'jxx',
		decontaminationTimer = 60*9 + 15,
		terroristTickets = 1,
		nexpharmaTickets = -99
	},
	['vietnam'] = {
		info = '设施已被绿皮怪物占领，机动紧急救援作战小组（MERCS）前来摧毁这里的怪物巢穴',
		author = 'Sharp-Shark',
		gamemode = 'greenskin',
		monsterSpawn = 'staff',
		autoJobRoleSequence = 'mxx',
		decontaminationTimer = 60*9 + 15,
		terroristTickets = -99,
		nexpharmaTickets = -1
	}
}

-- Sets the settings presets to the default
FG.settingsPresets = table.copy(FG.settingsPresetsDefault)

-- Sets the current settings to the default
FG.settings = table.copy(FG.settingsDefault)

-- Settings presets sent by clients
FG.settingsPresetsReceived = {}

-- Saves FG.settingsPresets to the config
function saveSettingsPresets()
	if not pcall(function ()
		File.Write(FG.path .. '/config.json', json.serialize(FG.settingsPresets))
	end) then
		print('[!] Error when saving settings presets to config!')
	end
end

-- Loads the config to FG.settingsPresets
function loadSettingPresets()
	if not pcall(function ()
		FG.settingsPresets = json.parse(File.Read(FG.path .. '/config.json'))
	end) then
		print('[!] Error when loading settings presets from config!')
	end
end

-- Load settings presets from config file if it exists or create one
if File.Exists(FG.path .. '/config.json') then
	loadSettingPresets()
else
	print('[!] Settings presets config file missing! Creating one...')
	saveSettingsPresets()
end

-- Receive config file from admins
Networking.Receive("loadClientPreset", function (message, client)
	print('[!] Received settings preset from a client.')
	if client.HasPermission(ClientPermissions.ConsoleCommands) then
		FG.settings = table.copy(FG.settingsDefault)
		local receivedSettings = json.parse(message.ReadString())
		for key, value in pairs(receivedSettings) do
			FG.settings[key] = value
		end
		messageClient(client, 'text-general', '你的预设配置已被服务器加载。')
	else
		local settingsPresetReceived = json.parse(message.ReadString())
		if settingsPresetReceived.author == nil then
			settingsPresetReceived.author = client.Name
		end
		FG.settingsPresetsReceived[client.Name] = {[message.ReadString()] = settingsPresetReceived}
		messageClient(client, 'text-general', '你的预设配置在服务器的提交列表中。')
	end
end)

FG.loadedFiles['settings'] = true